package io.renren.modules.meta.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.meta.dao.EventEntityMapper;
import io.renren.modules.meta.entity.EventEntity;
import io.renren.modules.meta.service.EventEntityService;
import io.renren.modules.meta.service.SubjectService;
import io.renren.modules.subject.dao.CarDao;
import io.renren.modules.subject.entity.BaseSubjectExtend;
import io.renren.modules.subject.entity.BaseSubjectTableInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("eventService")
public class EventEntityServiceImpl extends ServiceImpl<EventEntityMapper, EventEntity> implements EventEntityService {
    static HashMap<String, String> hashMap = new HashMap<>();
    @Autowired
    private SubjectService subjectService;

    static {
        hashMap.put("tb_phone_event", "tb_phone");
        hashMap.put("tb_car_event", "tb_car");
        hashMap.put("tb_penalty_persons_event", "tb_penalty_persons");
        hashMap.put("tb_fraudulent_persons_event", "tb_fraudulent_persons");
        hashMap.put("tb_criminal_strike_persons_event", "tb_criminal_strike_persons");
    }

    @Resource
    CarDao carDao;

    @Override
    public List<EventEntity> listAllByName(String name) {
        return baseMapper.selectAllByName(name);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<EventEntity> page = this.page(
                new Query<EventEntity>().getPage(params),
                new QueryWrapper<EventEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<BaseSubjectExtend> getSubjectsByEventId(Long id) {
        List<BaseSubjectExtend> subjects = new ArrayList<>();
        for (String middleTable : hashMap.keySet()) {
            String subjectTableName = hashMap.get(middleTable);
            List<BaseSubjectExtend> subjectsByTableName = baseMapper.selectSubjectsByEventId(id, middleTable, subjectTableName);
            subjects.addAll(subjectsByTableName);
        }
        return subjects;
    }

    @Override
    public List<BaseSubjectTableInfo> getSubjectTableInfoByEventId(Long id) {
        List<BaseSubjectTableInfo> result = new ArrayList<>();
        for (String middleTable : hashMap.keySet()) {
            BaseSubjectTableInfo baseSubjectTableInfo = new BaseSubjectTableInfo();
            String subjectTableName = hashMap.get(middleTable);
            Long subjectTableId = subjectService.getTableIdByTableName(subjectTableName);
            List<BaseSubjectExtend> list = baseMapper.selectSubjectsByEventId(id, middleTable, subjectTableName);
            for (BaseSubjectExtend baseSubjectExtend : list) {
                Long subjectId = baseSubjectExtend.getId();
                String name = baseSubjectExtend.getName();
                baseSubjectTableInfo.setSubjectName(name);
                baseSubjectTableInfo.setSubjectTable(subjectTableName);
                baseSubjectTableInfo.setSubjectId(subjectId);
                baseSubjectTableInfo.setSubjectTableId(subjectTableId);
                result.add(baseSubjectTableInfo);
            }
        }
        return result;
    }


}